class Api::V1::BeehivesController < Api::V1::BaseController
  before_action :authenticate_request!

  def index
    @beehives = apply_scopes(Beehive).all
    @beehives = paginate(@beehives)
    render json: @beehives, meta: paginate_meta(@beehives)
  end

  def create
    begin
      @beehive = Beehive.new(create_params)
      authorize @beehive
      @beehive.save!
      render json: @beehive, status: 201
    rescue ActiveRecord::StatementInvalid
      @beehive.errors.add(:exec_error, 'Contact Administrator')
      error_render(@beehive, 403)
    rescue => err
      error_render(err.record, 403)
    end
  end

  def show
    @beehive = Beehive.find(params[:id])
    render json: @beehive, status: 200
  end

  def update
    begin
      @beehive = Beehive.find(params[:id])
      @beehive.update_attributes!(create_params)
      render json: @beehive, status: 200
    rescue => err
      error_render(err.record, 403)
    end
  end

  def destroy
    begin
      @beehive = Beehive.find(params[:id])
      @beehive.destroy!
      render status: 204
    rescue ActiveRecord::RecordNotFound
      render status: 404
    rescue => err
      error_render(err.record, 403)
    end
  end

  private

  def create_params
    params_parse(params)
  end

end
